﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Plane : MonoBehaviour
{
    public GameObject bullet;
    public int id;
    public Vector3 velocity;
    public float maxVelocity;
    public Vector3 acc;
    private int[] _motorMask = new int[3];

    public Team cachedTeam {
        get {
            return GetComponent<Team>();
        }
    }


    // Start is called before the first frame update

    public void Boom()
    {
        GameObject.DestroyImmediate(gameObject);
    }

    public void setMotor(int dir, int mask)
    {
        _motorMask[dir] = mask;
    }

    public void Fire()
    {
        GameObject newBullet = GameObject.Instantiate(bullet);
        newBullet.transform.parent = transform.parent;
        newBullet.transform.localPosition = transform.localPosition + new Vector3(0, 0, 10);
        Bullet script = newBullet.GetComponent<Bullet>();
        script.cachedTeam.team = cachedTeam.team;
        script.creatorId = id;
        script.dir = new Vector3(0, 0, 1);
    }

    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        _DealWithRight(_motorMask[0], Time.deltaTime);
        _DealWithUp(_motorMask[1], Time.deltaTime);
        _DealWithForward(_motorMask[2], Time.deltaTime);

        Vector3 pos = transform.localPosition;
        pos.x += Time.deltaTime * velocity.x;
        pos.y += Time.deltaTime * velocity.y;
        pos.z += Time.deltaTime * velocity.z;
        transform.localPosition = pos;
    }

    private void _DealWithForward(int mask, float dt)
    {
        float a = acc.z * mask;
        if(mask == 0 && Mathf.Abs(velocity.z) > 0.0001) {
            a = Mathf.Abs(acc.z) * -Mathf.Sign(velocity.z);
        }
        if(a * velocity.z < 0)
        {
            //slow down first
            float dv = dt * a * 10;
            if(Mathf.Abs(dv) >= Mathf.Abs(velocity.z))
            {
                velocity.z = 0;
            } else {
                velocity.z += dv;
            }
        }
        else if(mask != 0)
        {
            //acc
            float dv = dt * a;
            if(Mathf.Abs(dv) > maxVelocity - Mathf.Abs(velocity.z))
            {
                velocity.z = Mathf.Sign(velocity.z) * maxVelocity;
            } else {
                velocity.z += dv;
            }
        }
    }
    private void _DealWithRight(int mask, float dt)
    {
        float a = acc.x * mask;
        if(mask == 0 && Mathf.Abs(velocity.x) > 0.0001) {
            a = Mathf.Abs(acc.x) * -Mathf.Sign(velocity.x);
        }
        if(a * velocity.x < 0)
        {
            //slow down first
            float dv = dt * a * 10;
            if(Mathf.Abs(dv) >= Mathf.Abs(velocity.x))
            {
                velocity.x = 0;
            } else {
                velocity.x += dv;
            }
        }
        else
        {
            //acc
            float dv = dt * a;
            if(Mathf.Abs(dv) > maxVelocity - Mathf.Abs(velocity.x))
            {
                velocity.x = Mathf.Sign(velocity.x) * maxVelocity;
            } else {
                velocity.x += dv;
            }
        }
    }
    private void _DealWithUp(int mask, float dt)
    {
        float a = acc.y * mask;
        if(mask == 0 && Mathf.Abs(velocity.y) > 0.0001) {
            a = Mathf.Abs(acc.y) * -Mathf.Sign(velocity.y);
        }
        if(a * velocity.y < 0)
        {
            //slow down first
            float dv = dt * a * 10;
            if(Mathf.Abs(dv) >= Mathf.Abs(velocity.y))
            {
                velocity.y = 0;
            } else {
                velocity.y += dv;
            }
        }
        else
        {
            //acc
            float dv = dt * a;
            if(Mathf.Abs(dv) > maxVelocity - Mathf.Abs(velocity.y))
            {
                velocity.y = Mathf.Sign(velocity.y) * maxVelocity;
            } else {
                velocity.y += dv;
            }
        }
    }
}
